package org.rency.mq.activemq.jms.request;

import org.rency.mq.activemq.constant.AcknowledgeMode;
import org.rency.mq.activemq.request.AbstractRequest;
import org.rency.mq.activemq.request.MQRequest;
import org.rency.mq.activemq.request.NotifyRequest;

/**
 * 用于系统间发送通知消息
 *
 * @author: Created by user_rcy@163.com on 2016/10/19.
 * @version: V1.0.0
 * @since JDK 1.8
 */
public class DefaultNotifyRequest<T> extends AbstractRequest implements NotifyRequest<T> {

    /**
     * 需要传递的消息对象
     */
    private T content;
    private String referenceId;

    public DefaultNotifyRequest(){}

    @Deprecated
    public DefaultNotifyRequest(Integer actionId, T content) {
        setActionId(actionId);
        this.content = content;
    }

    /**
     * 发送通知消息到指定队列
     * @param queueName	队列名称
     * @param content	消息内容
     */
    public DefaultNotifyRequest(String queueName, T content) {
        setDestination(queueName);
        this.content = content;
    }

    /**
     * 发送通知消息到指定目的地
     * @param destination	目的地名称
     * @param destinationType	目的地类型
     * @param content	消息内容
     */
    public DefaultNotifyRequest(String destination, int destinationType, T content) {
        setDestination(destination);
        setDestinationType(destinationType);
        this.content = content;
    }

    @Deprecated
    public DefaultNotifyRequest(Integer actionId, T content, int messageFormat) {
        this(actionId, content);
        setMessageFormat(messageFormat);
    }

    /**
     * 通过指定的格式发送通知消息到指定队列
     * @param queueName	队列名称
     * @param content	消息内容
     * @param messageFormat 消息格式
     */
    public DefaultNotifyRequest(String queueName, T content, int messageFormat) {
        setDestination(queueName);
        this.content = content;
    }

    /**
     * 通过指定的格式发送通知消息到指定目的地
     * @param destination	目的地名称
     * @param destinationType	目的地类型
     * @param content	消息内容
     * @param messageFormat 消息格式
     */
    public DefaultNotifyRequest(String destination, int destinationType, T content, int messageFormat) {
        setDestination(destination);
        setDestinationType(destinationType);
        this.content = content;
    }

    @Deprecated
    public DefaultNotifyRequest(Integer actionId, T content, int messageFormat, boolean transcated, int deliveryMode) {
        this(actionId, content, messageFormat);

        if(transcated){
            setAcknowledgeMode(AcknowledgeMode.SESSION_TRANSACTED);
        }

        setTransacted(transcated);
        setDeliveryMode(deliveryMode);
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    public String getReferenceId() {
        return referenceId;
    }

    public void setReferenceId(String referenceId) {
        this.referenceId = referenceId;
    }

    @Override
    public void setTransacted(boolean transacted) {
        setTransacted(transacted);
        if(transacted){
            setAcknowledgeMode(AcknowledgeMode.SESSION_TRANSACTED);
        }
    }

    public MQRequest deepCopy() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String toString() {
        return "DefaultNotifyRequest [content=" + content + ", referenceId="
                + referenceId + "[" + super.toString() + "]]";
    }
}
